package com.yida.gwj.finance.controller;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.yida.common.core.domain.R;
import com.yida.common.core.utils.PageUtils;
import com.yida.common.core.utils.poi.ExcelUtil;
import com.yida.common.core.web.controller.BaseController;
import com.yida.common.core.web.domain.AjaxResult;
import com.yida.common.core.web.page.TableDataInfo;
import com.yida.common.log.annotation.Log;
import com.yida.common.log.enums.BusinessType;
import com.yida.common.security.annotation.InnerAuth;
import com.yida.common.security.annotation.Logical;
import com.yida.common.security.annotation.RequiresPermissions;
import com.yida.gwj.api.domain.GwjFinanceAccountDetail;
import com.yida.gwj.api.domain.dto.GwjFinanceAccountDetailDTO;
import com.yida.gwj.api.domain.request.GwjFinanceAccountDetailRequest;
import com.yida.gwj.api.domain.request.RemoteAccountDetailForRateRequest;
import com.yida.gwj.api.domain.request.RemoteGwjFinanceAccountDetailRequest;
import com.yida.gwj.common.utils.GwjConvertUtil;
import com.yida.gwj.finance.service.IGwjFinanceAccountDetailService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * @author lqq
 * @date 2023-02-09
 */
@RestController
@RequestMapping("/accountDetail")
@Api(tags = "财务模块（分类帐流水）")
public class GwjFinanceAccountDetailController extends BaseController {

    @Resource
    private IGwjFinanceAccountDetailService gwjFinanceAccountDetailService;

    @ApiOperation("查询分类帐流水列表")
    @RequiresPermissions(value = {"gwj:finance:accountDetail:list", "gwj:basis:data:query"}, logical = Logical.OR)
    @GetMapping("/list")
    public TableDataInfo list(GwjFinanceAccountDetailRequest request) {
        GwjFinanceAccountDetail gwjFinanceAccountDetail = GwjConvertUtil.convert(request, GwjFinanceAccountDetail.class);
        startPage();
        List<GwjFinanceAccountDetail> list = gwjFinanceAccountDetailService.selectGwjFinanceAccountDetailList(gwjFinanceAccountDetail);
        return GwjConvertUtil.convertTable(getDataTable(list), GwjFinanceAccountDetailDTO.class);
    }

    @ApiOperation("导出分类帐流水列表")
    @RequiresPermissions("gwj:finance:accountDetail:export")
    @Log(title = "分类帐流水", businessType = BusinessType.EXPORT, systemName = "gwj")
    @PostMapping("/export")
    public void export(HttpServletResponse response, GwjFinanceAccountDetail gwjFinanceAccountDetail) {
        List<GwjFinanceAccountDetail> list = gwjFinanceAccountDetailService.selectGwjFinanceAccountDetailList(gwjFinanceAccountDetail);
        ExcelUtil<GwjFinanceAccountDetail> util = new ExcelUtil<GwjFinanceAccountDetail>(GwjFinanceAccountDetail.class);
        util.exportExcel(response, list, "分类帐流水数据");
    }

    @ApiOperation("获取分类帐流水详细信息")
    @RequiresPermissions("gwj:finance:accountDetail:query")
    @GetMapping(value = "/{accountDetailId}")
    public AjaxResult getInfo(@PathVariable("accountDetailId") Long accountDetailId) {
        return AjaxResult.success(gwjFinanceAccountDetailService.selectGwjFinanceAccountDetailByAccountDetailId(accountDetailId));
    }

    @ApiOperation("新增分类帐流水")
    @RequiresPermissions("gwj:finance:accountDetail:add")
    @Log(title = "分类帐流水", businessType = BusinessType.INSERT, systemName = "gwj")
    @PostMapping
    public AjaxResult add(@RequestBody GwjFinanceAccountDetail gwjFinanceAccountDetail) {
        return toAjax(gwjFinanceAccountDetailService.insertGwjFinanceAccountDetail(gwjFinanceAccountDetail));
    }

    @ApiOperation("修改分类帐流水")
    @RequiresPermissions("gwj:finance:accountDetail:edit")
    @Log(title = "分类帐流水", businessType = BusinessType.UPDATE, systemName = "gwj")
    @PutMapping
    public AjaxResult edit(@RequestBody GwjFinanceAccountDetail gwjFinanceAccountDetail) {
        return toAjax(gwjFinanceAccountDetailService.updateGwjFinanceAccountDetail(gwjFinanceAccountDetail));
    }

    @ApiOperation("删除分类帐流水")
    @RequiresPermissions("gwj:finance:accountDetail:remove")
    @Log(title = "分类帐流水", businessType = BusinessType.DELETE, systemName = "gwj")
    @DeleteMapping("/{accountDetailIds}")
    public AjaxResult remove(@PathVariable Long[] accountDetailIds) {
        return toAjax(gwjFinanceAccountDetailService.deleteGwjFinanceAccountDetailByAccountDetailIds(accountDetailIds));
    }

    /**
     * 新增分类帐流水
     */
    @InnerAuth
    @PostMapping("/inner")
    public R<Integer> insertGwjFinanceAccountDetail(@RequestBody GwjFinanceAccountDetail gwjFinanceAccountDetail) {
        return R.ok(gwjFinanceAccountDetailService.insertGwjFinanceAccountDetail(gwjFinanceAccountDetail));
    }

    /**
     * 新增分类帐流水
     */
    @InnerAuth
    @PostMapping("/innerBatch")
    public R<Integer> insertBatchGwjFinanceAccountDetail(@RequestBody ArrayList<GwjFinanceAccountDetail> gwjFinanceAccountDetails) {
        return R.ok(gwjFinanceAccountDetailService.insertBatchGwjFinanceAccountDetail(gwjFinanceAccountDetails));
    }

    /**
     * 修改分类帐流水
     */
    @InnerAuth
    @PutMapping("/inner")
    public R<Integer> updateGwjFinanceAccountDetail(@RequestBody GwjFinanceAccountDetail gwjFinanceAccountDetail) {
        return R.ok(gwjFinanceAccountDetailService.updateGwjFinanceAccountDetail(gwjFinanceAccountDetail));
    }

    /**
     * 批量修改分类帐流水
     */
    @InnerAuth
    @PostMapping("/innerEditBatch")
    public R<Integer> updateBatchGwjFinanceAccountDetail(@RequestBody ArrayList<GwjFinanceAccountDetail> details) {
        return R.ok(gwjFinanceAccountDetailService.updateBatchGwjFinanceAccountDetail(details));
    }

    /**
     * 根据业务ID删除分类帐信息
     */
    @InnerAuth
    @DeleteMapping("/inner/{bizId}")
    public R<Integer> deleteGwjFinanceAccountDetailByBizId(@PathVariable("bizId") Long bizId) {
        return R.ok(gwjFinanceAccountDetailService.deleteGwjFinanceAccountDetailByBizId(bizId));
    }

    /**
     * 根据业务ID查询总账流水
     */
    @InnerAuth
    @GetMapping("/inner/list/{bizId}")
    public R<List<GwjFinanceAccountDetail>> selectGwjFinanceAccountDetailListByBizId(@PathVariable("bizId") Long bizId) {
        return R.ok(gwjFinanceAccountDetailService.selectGwjFinanceAccountDetailListByBizId(bizId));
    }

    /**
     * 查询总账流水
     */
    @InnerAuth
    @PostMapping("/inner/list")
    public R<List<GwjFinanceAccountDetail>> selectGwjFinanceAccountDetailList(@RequestBody GwjFinanceAccountDetail gwjFinanceAccountDetail) {
        return R.ok(gwjFinanceAccountDetailService.selectGwjFinanceAccountDetailList(gwjFinanceAccountDetail));
    }

    /**
     * 批量新增分类帐流水
     *
     * @param accountDetailArrayList 分类账信息明细
     * @return
     */
    @InnerAuth
    @PostMapping("/innerSaveBatch")
    R<Integer> insertBatchGwjFinanceAccountDetailInner(@RequestBody ArrayList<GwjFinanceAccountDetail> accountDetailArrayList) {
        return R.ok(gwjFinanceAccountDetailService.insertBatchGwjFinanceAccountDetailInner(accountDetailArrayList));
    }

    /**
     * 查询分账列表
     *
     * @param detailRequest
     * @return
     */
    @PostMapping("/getGwjFinanceAccountDetailList")
    R<List<GwjFinanceAccountDetail>> getGwjFinanceAccountDetailList(@RequestBody RemoteGwjFinanceAccountDetailRequest detailRequest) {
        return R.ok(gwjFinanceAccountDetailService.getGwjFinanceAccountDetailList(detailRequest));
    }

    /**
     * 查询分账列表
     *
     * @param detailRequest
     * @return
     */
    @PostMapping("/getGwjFinanceAccountDetailPage")
    R<TableDataInfo> getGwjFinanceAccountDetailPage(@RequestBody RemoteGwjFinanceAccountDetailRequest detailRequest) {
        PageUtils.startPage(detailRequest.getPageNum(), detailRequest.getPageSize());
        return R.ok(getDataTable(gwjFinanceAccountDetailService.getGwjFinanceAccountDetailList(detailRequest)));
    }

    /**
     * 查询分账列表 用于计息
     *
     * @param request
     * @return
     */
    @PostMapping("/getListForRate")
    R<List<GwjFinanceAccountDetail>> getListForRate(@RequestBody RemoteAccountDetailForRateRequest request) {
        return R.ok(gwjFinanceAccountDetailService.getListForRate(request));
    }

    @InnerAuth
    @PostMapping("/deleteGwjFinanceAccountDetail")
    R<Integer> deleteGwjFinanceAccountDetail(@RequestBody GwjFinanceAccountDetail financeAccountDetail) {
        return R.ok(gwjFinanceAccountDetailService.deleteGwjFinanceAccountDetail(financeAccountDetail));
    }

    /**
     * 获取分类账统计信息
     * 
     * @param financeAccountDetail
     * @return
     */
    @InnerAuth
    @PostMapping("/selectGwjFinanceAccountDetailStatistics")
    R<Map<String, BigDecimal>> selectGwjFinanceAccountDetailStatistics(@RequestBody GwjFinanceAccountDetail financeAccountDetail) {
        return R.ok(gwjFinanceAccountDetailService.selectGwjFinanceAccountDetailStatistics(financeAccountDetail));
    }

    @ApiOperation("查询分类帐流水列表")
    @RequiresPermissions(value = {"gwj:finance:accountDetail:list", "gwj:basis:data:query"}, logical = Logical.OR)
    @GetMapping("/detailList")
    public TableDataInfo detailList(GwjFinanceAccountDetailRequest request) {
        GwjFinanceAccountDetail gwjFinanceAccountDetail = GwjConvertUtil.convert(request, GwjFinanceAccountDetail.class);
        startPage();
        List<GwjFinanceAccountDetail> list = gwjFinanceAccountDetailService.selectGwjFinanceAccountDetailListData(gwjFinanceAccountDetail);
        return getDataTable(list);
    }

}
